﻿bob.ns.setNs('bob.prop.event.tests', {

    TestEvent: function () {

        this.test_NotifierProperty = function () {

            //similar to Property testing:

            //read-write
            var value = undefined;
            var change = false;
            var p = new bob.prop.event.NotifierProperty(value);
            p.change.add_handler(function () { change = true; });
            bob.assert.equals(value, p.get_value(), '1');
            value = 1;
            change = false;
            p.set_value(value);
            bob.assert.equals(value, p.get_value(), '2');
            bob.assert.isTrue(change, '3');
            value = undefined;
            change = false;
            p.set_value(value);
            bob.assert.equals(value, p.get_value(), '4');
            bob.assert.isTrue(change, '5');

            //read-only
            value = 1;
            change = false;
            p = new bob.prop.event.NotifierProperty(value, true);
            p.change.add_handler(function () { change = true; });
            var setter = p.get_setter();
            bob.assert.equals(value, p.get_value(), '6');
            bob.assert.isUndefined(p.set_value, '7');
            bob.assert.isUndefined(p.get_setter, '8');
            bob.assert.isFalse(change, '9');

            value = 2;
            change = false;
            setter(value);
            bob.assert.equals(value, p.get_value(), '10');
            bob.assert.isUndefined(p.set_value, '11');
            bob.assert.isUndefined(p.get_setter, '12');
            bob.assert.isTrue(change, '13');

            //write-only
            value = 1;
            change = false;
            p = new bob.prop.event.NotifierProperty(value, false, true);
            p.change.add_handler(function () { change = true; });
            var getter = p.get_getter();
            bob.assert.equals(value, getter(), '14');
            bob.assert.isUndefined(p.get_value, '15');
            bob.assert.isUndefined(p.get_getter, '16');
            bob.assert.isFalse(change, '17');

            value = 2;
            change = false;
            p.set_value(value);
            bob.assert.equals(value, getter(), '18');
            bob.assert.isUndefined(p.get_value, '19');
            bob.assert.isUndefined(p.get_getter, '20');
            bob.assert.isTrue(change, '21');

        };


        this.test_Observer = function () {

            //similar to Property testing:

            //read-write
            var value = undefined;
            var change = false;
            var p = new bob.prop.event.Observer(value);
            p.change.add_handler(function () { change = true; });
            bob.assert.equals(value, p.get_value(), '1');
            value = 1;
            change = false;
            p.set_value(value);
            bob.assert.equals(value, p.get_value(), '2');
            bob.assert.isTrue(change, '3');
            value = undefined;
            change = false;
            p.set_value(value);
            bob.assert.equals(value, p.get_value(), '4');
            bob.assert.isTrue(change, '5');

            //read-only
            value = 1;
            change = false;
            p = new bob.prop.event.Observer(value, true);
            p.change.add_handler(function () { change = true; });
            var setter = p.get_setter();
            bob.assert.equals(value, p.get_value(), '6');
            bob.assert.isUndefined(p.set_value, '7');
            bob.assert.isUndefined(p.get_setter, '8');
            bob.assert.isFalse(change, '9');

            value = 2;
            change = false;
            setter(value);
            bob.assert.equals(value, p.get_value(), '10');
            bob.assert.isUndefined(p.set_value, '11');
            bob.assert.isUndefined(p.get_setter, '12');
            bob.assert.isTrue(change, '13');

            //write-only
            value = 1;
            change = false;
            p = new bob.prop.event.Observer(value, false, true);
            p.change.add_handler(function () { change = true; });
            var getter = p.get_getter();
            bob.assert.equals(value, getter(), '14');
            bob.assert.isUndefined(p.get_value, '15');
            bob.assert.isUndefined(p.get_getter, '16');
            bob.assert.isFalse(change, '17');

            value = 2;
            change = false;
            p.set_value(value);
            bob.assert.equals(value, getter(), '18');
            bob.assert.isUndefined(p.get_value, '19');
            bob.assert.isUndefined(p.get_getter, '20');
            bob.assert.isTrue(change, '21');

        };

    }

});